package cn.com.css.taxInspection.attention.controller;

import cn.com.css.taxInspection.attention.entity.XazbNsr;
import cn.com.css.taxInspection.attention.entity.XazbNsrComplete;
import cn.com.css.taxInspection.attention.entity.ZbxaIndustryEntity;
import cn.com.css.taxInspection.attention.entity.ZbxaXafaEntity;
import cn.com.css.taxInspection.attention.service.XazbNsrCompleteService;
import cn.com.css.taxInspection.attention.service.ZbxaIndustryService;
import cn.com.css.taxInspection.attention.service.ZbxaXafaService;
import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.common.constants.TaxConstants;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.gt3.entity.DmGySwjgEntity;
import cn.com.css.taxInspection.gt3.entity.DmGySwryEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/")
public class ZbxaXafaController {
    @Resource
    private XazbNsrCompleteService xazbNsrCompleteService;
    @Resource
    private ZbxaXafaService zbxaXafaService;
    @Resource
    private HttpSessionService httpSessionService;

    @Resource
    private ZbxaIndustryService zbxaIndustryService;

    @Resource
    private DmGySwryService dmGySwryService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/zbxaXafa")
    public String zbxaXafa() {
        return "zbxaxafa/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("zbxaXafa/add")
    @ResponseBody
    public DataResult add(@RequestBody ZbxaXafaEntity zbxaXafa){
        zbxaXafa.setCreateTime(new Date());
        zbxaXafa.setUpdateTime(null);
        zbxaXafa.setStatus("0");
        zbxaXafa.setLrrDm(httpSessionService.getCurrentUserId());
        zbxaXafa.setSwjgDm(httpSessionService.getCurrentDeptId().substring(0,9)+"00");
        zbxaXafaService.save(zbxaXafa);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("zbxaXafa/delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        zbxaXafaService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("zbxaXafa/update")
    @ResponseBody
    public DataResult update(@RequestBody ZbxaXafaEntity zbxaXafa){
        zbxaXafaService.updateById(zbxaXafa);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("zbxaXafa/listByPage")
    @ResponseBody
    public DataResult findListByPage(@RequestBody ZbxaXafaEntity zbxaXafa){
        Page page = new Page(zbxaXafa.getPage(), zbxaXafa.getLimit());
        LambdaQueryWrapper<ZbxaXafaEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ZbxaXafaEntity::getLrrDm, httpSessionService.getCurrentUserId());
        IPage<ZbxaXafaEntity> iPage = zbxaXafaService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "审批查询分页数据")
    @PostMapping("zbxaXafa/listByPagesp")
    @ResponseBody
    public DataResult listByPagesp(@RequestBody ZbxaXafaEntity zbxaXafa){
        String currentRolesId = httpSessionService.getCurrentRolesId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String substring = currentDeptId.substring(0, 9);
        String status = "";
        if(currentRolesId.contains(TaxConstants.SP_XAKZGW_ID)){//选案科长 查11
            status = "11";
        }else if (currentRolesId.contains(TaxConstants.SP_XAZGJZGW_ID)){//选案主管局长12
            status = "12";
        }else if (currentRolesId.contains(TaxConstants.SP_XAJZGW_ID)){//局长13
            status = "13";
        }
        Page page = new Page(zbxaXafa.getPage(), zbxaXafa.getLimit());
        LambdaQueryWrapper<ZbxaXafaEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ZbxaXafaEntity::getStatus, status);
        queryWrapper.eq(ZbxaXafaEntity::getSwjgDm,substring+"00");
        queryWrapper.orderByAsc(ZbxaXafaEntity::getUpdateTime);
        IPage<ZbxaXafaEntity> iPage = zbxaXafaService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @PostMapping("zbxaXafa/getZdhyS")
    @ResponseBody
    public DataResult getcurrentswjgjcks() {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String substring = currentDeptId.substring(0, 9);
        LambdaQueryWrapper<ZbxaIndustryEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ZbxaIndustryEntity::getStatus, "1");
        queryWrapper.eq(ZbxaIndustryEntity::getSwjgDm, substring+"00");
        List<ZbxaIndustryEntity> list =zbxaIndustryService.list(queryWrapper);
        return DataResult.success(list);
    }
    @PostMapping("zbxaXafa/getXafas")
    @ResponseBody
    public DataResult getXafas() {
        String userId = httpSessionService.getCurrentUserId();
        LambdaQueryWrapper<ZbxaXafaEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ZbxaXafaEntity::getStatus, "2");
        queryWrapper.eq(ZbxaXafaEntity::getLrrDm, userId);
        List<ZbxaXafaEntity> list =zbxaXafaService.list(queryWrapper);
        return DataResult.success(list);
    }

    @ApiOperation(value = "撤回")
    @PutMapping("zbxaXafa/chehui")
    @ResponseBody
    public DataResult chehui(@RequestBody ZbxaXafaEntity zbxaXafa){
        UpdateWrapper<ZbxaXafaEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", zbxaXafa.getId()); // 设置条件，例如根据 id 更新
        ZbxaXafaEntity entity = new ZbxaXafaEntity();
        entity.setId(zbxaXafa.getId());
        entity.setStatus("0");
        entity.setUpdateTime(new Date());
        zbxaXafaService.update(entity, updateWrapper);
        return DataResult.success();
    }

    @ApiOperation(value = "归档")
    @PutMapping("zbxaXafa/guidang")
    @ResponseBody
    public DataResult guidang(@RequestBody ZbxaXafaEntity zbxaXafa){
        UpdateWrapper<ZbxaXafaEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", zbxaXafa.getId()); // 设置条件，例如根据 id 更新
        ZbxaXafaEntity entity = new ZbxaXafaEntity();
        entity.setId(zbxaXafa.getId());
        entity.setStatus("4");
        entity.setUpdateTime(new Date());
        zbxaXafaService.update(entity, updateWrapper);
        return DataResult.success();
    }

    @ApiOperation(value = "作废")
    @PutMapping("zbxaXafa/zuofei")
    @ResponseBody
    public DataResult zuofei(@RequestBody ZbxaXafaEntity zbxaXafa){
        UpdateWrapper<ZbxaXafaEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", zbxaXafa.getId()); // 设置条件，例如根据 id 更新
        ZbxaXafaEntity entity = new ZbxaXafaEntity();
        entity.setId(zbxaXafa.getId());
        entity.setStatus("5");
        entity.setUpdateTime(new Date());
        zbxaXafaService.update(entity, updateWrapper);
        return DataResult.success();
    }

    @ApiOperation(value = "报批")
    @PutMapping("zbxaXafa/baopi")
    @ResponseBody
    public DataResult baopi(@RequestBody ZbxaXafaEntity zbxaXafa){
        UpdateWrapper<ZbxaXafaEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", zbxaXafa.getId()); // 设置条件，例如根据 id 更新
        ZbxaXafaEntity entity = new ZbxaXafaEntity();
        entity.setId(zbxaXafa.getId());
        entity.setStatus("11");
        entity.setUpdateTime(new Date());
        zbxaXafaService.update(entity, updateWrapper);
        return DataResult.success();
    }


    @ApiOperation(value = "审批通过")
    @PutMapping("zbxaXafa/sptg")
    @ResponseBody
    public DataResult sptg(@RequestBody ZbxaXafaEntity zbxaXafa){
        String currentRolesId = httpSessionService.getCurrentRolesId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String status = "";
        if(currentRolesId.contains(TaxConstants.SP_XAKZGW_ID)){//选案科长 查11
            status = "12";
        }else if (currentRolesId.contains(TaxConstants.SP_XAZGJZGW_ID)){//选案主管局长12
            status = "13";
        }else if (currentRolesId.contains(TaxConstants.SP_XAJZGW_ID)){//局长13
            status = "2";
        }
        UpdateWrapper<ZbxaXafaEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", zbxaXafa.getId()); // 设置条件，例如根据 id 更新
        ZbxaXafaEntity entity = new ZbxaXafaEntity();
        entity.setId(zbxaXafa.getId());
        entity.setStatus(status);
        entity.setUpdateTime(new Date());
        zbxaXafaService.update(entity, updateWrapper);
        return DataResult.success();
    }

    @ApiOperation(value = "审批不通过")
    @PutMapping("zbxaXafa/spbtg")
    @ResponseBody
    public DataResult spbtg(@RequestBody ZbxaXafaEntity zbxaXafa){
        ZbxaXafaEntity entity = new ZbxaXafaEntity();
        String currentRolesId = httpSessionService.getCurrentRolesId();
        String status = "";
        if(currentRolesId.contains(TaxConstants.SP_XAKZGW_ID)){//选案科长 查11
            status = "61";
            entity.setKzReason(zbxaXafa.getKzReason());
        }else if (currentRolesId.contains(TaxConstants.SP_XAZGJZGW_ID)){//选案主管局长12
            status = "62";
            entity.setZgjzReason(zbxaXafa.getKzReason());
        }else if (currentRolesId.contains(TaxConstants.SP_XAJZGW_ID)){//局长13
            status = "63";
            entity.setJzReason(zbxaXafa.getKzReason());
        }
        UpdateWrapper<ZbxaXafaEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", zbxaXafa.getId()); // 设置条件，例如根据 id 更新
        entity.setId(zbxaXafa.getId());
        entity.setStatus(status);
        entity.setUpdateTime(new Date());
        zbxaXafaService.update(entity, updateWrapper);
        return DataResult.success();
    }
}
